#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e5 + 10;
vector<PII> edges[N];
bool st[N]; 

int n, m;

void dfs(int u)
{
	st[u] = true;
	cout << u << " ";
	for(auto t : edges[u])
	{
		int v = t.first, w = t.second;
		if(!st[v]) dfs[v];
	}
}

void bfs(int u)
{
	queue<int> q;
	q.push(u);
	st[u] = true;
	while(q.size())
	{
		auto t = q.front(); q.pop();
		cout << t << " ";
		for(auto a : edges[t])
		{
			int v = a.first, w = a.second;
			if(!st[v])
			{
				q.push(v);
				st[v] = true;
			}
		}
	}
}

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= m; i++)
	{
		int a, b, c; cin >> a >> b >> c;
		edges[a].push_back({b, c});
		edges[b].push_back({a, c});
	}
	dfs(1);
	bfs(1);
	return 0;
}


